import httpRequest from './http-request'
import md5 from 'js-md5'

const pathPrefix = '/apis'
export function getLoginCode(): Promise<any> {
  return httpRequest.get(`${pathPrefix}/account/code`, { time: new Date().getTime() })
}

export function login(
  userName: string,
  password: string,
  codeKey: string,
  code: string
): Promise<any> {
  return httpRequest.post(`${pathPrefix}/account/login`, {
    username: userName,
    password: md5(password),
    key: codeKey,
    code
  })
}

export function loginWithRunworkToken(param: any): Promise<any> {
  return httpRequest.post(`${pathPrefix}/account/rwork_token`, param)
}

export function searchAgents(
  pageIndx?: number,
  pageSize?: number,
  name?: string,
  params?: any
): Promise<any> {
  const param: Record<string, any> = {}
  if (pageIndx) {
    param['current_page'] = pageIndx
  }
  if (pageSize) {
    param['page_size'] = pageSize
  }
  if (name) {
    param['name'] = name
  }

  if (params) {
    Object.assign(param, params)
  }
  return httpRequest.get(`${pathPrefix}/api/agent/agent/list`, param)
}

export function getMyAIAgents(): Promise<any> {
  return httpRequest.get(`${pathPrefix}/api/agent/agent/my_agent_list`)
}

export function getAgentDetailById(agentId: string): Promise<any> {
  return httpRequest.get(`${pathPrefix}/api/agent/agent/detail`, { id: agentId })
}

export function getChatHistory(): Promise<any> {
  return httpRequest.get(`${pathPrefix}/api/agent/chat/history`)
}

export function getMessageHistory(param?: any): Promise<any> {
  return httpRequest.get(`${pathPrefix}/api/agent/chat/detail`, param)
}

export function startAIGC(param?: any): Promise<any> {
  return httpRequest.post(`${pathPrefix}/api/agent/chat/send`, param)
}

export function fetchAIGCContent(param?: any): Promise<any> {
  return httpRequest.post(`${pathPrefix}/api/agent/chat/replay`, param)
}
